package top.yeyusmile.controller;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.yeyusmile.MainApp;
import top.yeyusmile.common.RobotConfig;
//import top.yeyusmile.common.TypeConstant;


import top.yeyusmile.service.ConvenientService;
import top.yeyusmile.service.DianGeService;


import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * @author 夜雨
 * @Web www.yeyusmile.top
 * @date 2022/3/17 8:15
 */
@Slf4j
@RequestMapping("/config")
@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class RobotConfigController {


    @Autowired
    private RobotConfig robotConfig;


    /**
     * @param s 1关  0关  默认开
     * @return
     */
    @ResponseBody
    @RequestMapping("/flashPicSwitch")
    public String flashPicSwitch(@RequestParam(value = "s", defaultValue = "1") Integer s) {
        if (s == 1) robotConfig.setFlashPicSwitch(true);
        else robotConfig.setFlashPicSwitch(false);
        return "设置成功:" + robotConfig.getFlashPicSwitch();
    }

    /**
     * @param s 1关  0关  默认开
     * @return
     */
    @ResponseBody
    @RequestMapping("/goBackcSwitch")
    public String goBackcSwitch(@RequestParam(value = "s", defaultValue = "1") Integer s) {
        if (s == 1) robotConfig.setGoBackcSwitch(true);
        else robotConfig.setGoBackcSwitch(false);
        return "设置成功:" + robotConfig.getGoBackcSwitch();
    }

    /**
     * @param s 1关  0关  默认开
     * @return
     */
    @ResponseBody
    @RequestMapping("/addGroupSwitch")
    public String addGroupSwitch(@RequestParam(value = "s", defaultValue = "1") Integer s) {
        if (s == 1) robotConfig.setAddGroupSwitch(true);
        else robotConfig.setAddGroupSwitch(false);
        return "设置成功:" + robotConfig.getAddGroupSwitch();
    }

    /**
     * @param s 1关  0关  默认开
     * @return
     */
    @ResponseBody
    @RequestMapping("/addFrendSwitch")
    public String addFrendSwitch(@RequestParam(value = "s", defaultValue = "1") Integer s) {
        if (s == 1) robotConfig.setAddFrendSwitch(true);
        else robotConfig.setAddFrendSwitch(false);
        return "设置成功:" + robotConfig.getAddFrendSwitch();
    }

    /**
     * @param group
     * @return
     */
    @ResponseBody
    @RequestMapping("/addGroupManger")
    public String addGroupManger(@RequestParam(value = "group") String group) {
        if (group == null || "".equals(group)) {
            return "增加失败:";
        } else {
            robotConfig.getGroupManger().add(group);
            return "增加群号申请自通过成功:" + group;
        }
    }

    /**
     * @param group
     * @return
     */
    @ResponseBody
    @RequestMapping("/delGroupManger")
    public String delGroupManger(@RequestParam(value = "group") String group) {
        if (group == null || "".equals(group)) {
            return "删除失败:";
        } else {
            robotConfig.delGroup(group);
            return "删除群管自申请通过成功:" + group;
        }
    }


    /**
     * 状态
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/setRobot")
    public String setRobot(@RequestParam(value = "robot") String robot) {
        if (robot == null || "".equals(robot)) {
            return "增加失败:";
        } else {
            robotConfig.setRobotQQ(robot);
            return "设置成功：" + robotConfig.getRobotQQ();
        }
    }


    /**
     * 修改配置 通用
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/modify")
    public String addRobot(RobotConfig config) {
        if (config != null) {
            if (config.getHost() != null && !"".equals(config.getHost()))
                robotConfig.setHost(config.getHost());
            if (config.getPort() != null && !"".equals(config.getPort()))
                robotConfig.setPort(config.getPort());
            if (config.getToken() != null && !"".equals(config.getToken()))
                robotConfig.setToken(config.getToken());
            if (config.getBlackWord() != null && !"".equals(config.getBlackWord()))
                robotConfig.setBlackWord(config.getBlackWord());
            if (config.getOwners() != null && !"".equals(config.getOwners()))
                robotConfig.setOwners(config.getOwners());
            //....
            return "修改完成";
        } else {
            return "修改失败";
        }
    }


    /**
     * 保存配置
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/save")
    public String saveConfig(HttpServletRequest request) {
        {

            File file = new File(this.robotConfig.getSaveConfigPath());
            // 如果保存文件的地址不存在，就先创建目录
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                OutputStream out = new FileOutputStream(file);
                String json = new Gson().toJson(robotConfig);
                out.write(json.getBytes(StandardCharsets.UTF_8));
                return "success";
            } catch (Exception e) {
                e.printStackTrace();
                return "error";
            }
        }
    }



    /**
     * 状态
     *
     * @return
     */
    @RequestMapping("/status")
    public RobotConfig delGroupManger() {
        return robotConfig;
    }


}
